package question3;

import java.io.*;

/**
 * @author Sharda Rallabhandy
 * @version 1.0
 */

/**
 * The Class Stutter.
 */
public class Stutter {
	/**
	 * The main method. Get the file name from the first argument or keyboard.
	 * Call checkStutter. If no file name is given, alert the user.
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String args[]) {
		// Print out header to consnole

// The code is modified to take two lines as input so that the code can be tested for key functionality
		// the lines are taken as line 1 and line 1 as if they are read from buffered reader
				String input1 = "World Hello";
				String input2 = null;
				try {
					checkStutter(input1,input2);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

	}

	/**
	 * Check stutter.
	 * 
	 * Reads the input file one line, then one word at a time. Looks for
	 * repeated words and informs users.
	 * 
	 * @param input
	 *            the input file argument
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public static void checkStutter(String input1, String input2) throws IOException {
		int numberOfLines = 1;
		String line = null;
		String lastWord = null;

		// Get the first line from the file argument
		line = input1;
System.out.println("edge 1-2");
		// Keep reading and analyzing lines until end of file
		while (line != null) {
			System.out.println("edge 2-3");
			// Remove all non alpha-numeric characters
			line = line.replaceAll("[^a-zA-Z0-9 ]", "");
			// Convert the input line to lower case
			line = line.toLowerCase();

			// Splits the line into words and puts in an array of strings
			String[] words = line.split(" ");

			// Compare with the last word on the previous line
			if (words[0].equals(lastWord)){
				System.out.println("Repeated word on line " + numberOfLines
						+ ": " + words[0]);
				System.out.println("edge 4-5");
			}
			// Stop before the end, nothing to compare the last word with
			for (int i = 0; i < (words.length - 1); i++) {
				System.out.println("edge 6-7");
				// Check to see if the current and subsequent words are the same
				if (words[i].equals(words[i + 1])){
					System.out.println("Repeated word on line " + numberOfLines
							+ ": " + words[i]);
					System.out.println("edge 7-8");
					}
			}

			// Save last word in the line
			lastWord = words[words.length - 1];
			// Get the next line from the file argument
			if (numberOfLines >1){
				line = null;
			}else{
			line = input2;}
			// Increment line counter
			numberOfLines++;
		}
	}
}